package com.uit.se6.mydiary.note;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.database.Cursor;

import com.uit.se6.mydiary.BaseActivity;
import com.uit.se6.mydiary.data.MySharedPreference;
import com.uit.se6.mydiary.system.UserSetting;
import com.uit.se6.mydiary.util.UtilTime;

public class NoteFactory {
	public static void createNote(BaseActivity activity,
			NotesDbAdapter notesAdapter, String year, String month, String day) {
		int noteID = UtilTime.serialize(year, month, day);
		String start = activity.getSharedPreference().getString(
				MySharedPreference.KEY_NOTE_START_WITH,
				UserSetting.NOTE_START_WITH);
		notesAdapter.createNote(noteID, start);
	}

	public static void createNote(BaseActivity activity,
			NotesDbAdapter notesAdapter, int year, int month, int day) {
		int noteID = UtilTime.serialize(year, month, day);
		String start = activity.getSharedPreference().getString(
				MySharedPreference.KEY_NOTE_START_WITH,
				UserSetting.NOTE_START_WITH);
		notesAdapter.createNote(noteID, start);
	}

	public static Cursor fetchAllNote(NotesDbAdapter notesAdapter) {
		return notesAdapter.fetchAllNotes();
	}

	public static Cursor fetchNote(NotesDbAdapter notesAdapter, int noteID) {
		return notesAdapter.fetchNote(noteID);
	}

	@SuppressLint("UseSparseArrays")
	public static HashMap<Integer, ArrayList<Integer>> fetchNoteStartWith(
			NotesDbAdapter notesAdapter, int year, int month) {
		HashMap<Integer, ArrayList<Integer>> result = new HashMap<Integer, ArrayList<Integer>>();
		ArrayList<Integer> notes = new ArrayList<Integer>();
		ArrayList<Integer> imgs = new ArrayList<Integer>();
		ArrayList<Integer> emo = new ArrayList<Integer>();
		// --------------------------------------------------------
		String startStringID = String.valueOf(year);
		if (month < 10) {
			startStringID += "0" + month;
		} else
			startStringID += String.valueOf(month);
		Cursor c = notesAdapter.fetchNoteStartWith(Integer
				.valueOf(startStringID));
		if (c.moveToFirst()) {
			do {
				int value = c.getInt(c
						.getColumnIndexOrThrow(NotesDbAdapter.KEY_ID));
				// value format: 2014 02 14
				int d = value % 100;
				notes.add(d);
				Cursor imgCursor = notesAdapter.fetchImageByNote(value);
				if (imgCursor.moveToFirst()) {
					imgs.add(d);
				}

			} while (c.moveToNext());
		}
		result.put(0, notes);
		result.put(1, imgs);
		result.put(2, emo);

		return result;
	}

	public static boolean updateNote(NotesDbAdapter notesAdapter, int noteID,
			String body) {
		return notesAdapter.updateNote(noteID, body);
	}

	public static boolean createImage(NotesDbAdapter notesAdapter, int noteID,
			String imagePath) {
		return notesAdapter.createImage(imagePath, noteID);
	}

	public static Cursor fetchImageByNote(NotesDbAdapter notesAdapter,
			int noteID) {
		return notesAdapter.fetchImageByNote(noteID);
	}

	public static Cursor fetchImageByPath(NotesDbAdapter notesAdapter,
			String path) {
		return notesAdapter.fetchImageByPath(path);
	}

	public static boolean deleteImage(NotesDbAdapter notesAdapter,
			String imagePath, int noteID) {
		return notesAdapter.deleteImage(imagePath, noteID);
	}

	public static void deleteAll(NotesDbAdapter noteAdapter) {
		noteAdapter.deleteAll();
	}

	public static boolean deleteNote(NotesDbAdapter noteAdapter, int noteId) {
		noteAdapter.deleteAllImageByNote(noteId);
		return noteAdapter.deleteNote(noteId);
	}

	public static Cursor fetchAllImage(NotesDbAdapter notesAdapter) {
		return notesAdapter.fetchAllImageString();
	}
}
